package com.gfxy.modules.index.controller;

import com.alibaba.fastjson.JSONObject;
import com.gfxy.modules.common.entity.CurrentDate;
import com.gfxy.modules.common.service.impl.CurrentDateService;
import com.gfxy.modules.educationandservice.entity.EducationAndService;
import com.gfxy.modules.educationandservice.service.IEducationAndServiceService;
import com.gfxy.modules.index.service.AssessmentService;
import com.gfxy.modules.base.vo.TeacherBaseInfoVo;
import com.gfxy.modules.classtime.entity.ClasstimeScore;
import com.gfxy.modules.classtime.service.IClasstimeScoreService;
import com.gfxy.modules.items.service.DictTranslater;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.SpringContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: lijixin 2021/10/21 9:06 下午
 */
@Api(tags="教师结果详情报表")
@RestController
@RequestMapping("index/TeacherYearAssessment")
@Slf4j
public class TeacherYearAssessmentServiceController {

    private static final String projectId="1";

    @Autowired
    private AssessmentService assessmentService;
    @Autowired
    private IClasstimeScoreService classtimeScoreService;
    @Autowired
    private CurrentDateService currentDateService;
    @Autowired
    protected DictTranslater dictTranslater;
    @Autowired
    private IEducationAndServiceService educationAndServiceService;


    @AutoLog(value = "根据用户名查询用户课时积分")
    @ApiOperation(value="根据用户名查询用户课时积分", notes="根据用户名查询用户课时积分")
    @GetMapping(value = "/getClasstimeScoreByDateAndUsername")
    public Result<ClasstimeScore> getClasstimeScoreCurrentDateByUsername(
            @RequestParam(name="date",required=true) String date,
            @RequestParam(name="userName",required=true) String userName,
                                                            HttpServletRequest req) {
        log.debug("开始查询教师[{}]的课时积分", userName);
        ClasstimeScore classtimeScore = classtimeScoreService.queryByDateAndTeacherId(date,userName);
        return Result.OK(classtimeScore);
    }

    @AutoLog(value = "根据用户名查询社会与服务积分")
    @ApiOperation(value="根据用户名查询社会与服务积分", notes="根据用户名查询社会与服务积分")
    @GetMapping(value = "/getEducationAndServiceServiceByDateAndUsername")
    public Result<List<EducationAndService>> getEducationAndServiceServiceByDateAndUsername(
            @RequestParam(name="date",required=true) String date,
            @RequestParam(name="userName",required=true) String userName,
            HttpServletRequest req) {
        log.debug("开始查询教师[{}]的课时积分", userName);
        List<EducationAndService> result = educationAndServiceService.queryEducationAndServiceByDateAndTeacherId(date, userName);
        return Result.OK(result);
    }





    @RequestMapping(value = "/exportDoc")
    public ModelAndView exportDoc( @RequestParam(name="teacherId") String teacherId,
                                   HttpServletRequest request) throws Exception {
        try{
            CurrentDate currentDate = currentDateService.getCurrentDate();
            List<TeacherBaseInfoVo> teacherBaseInfos = assessmentService.getTeacherBaseInfo(teacherId);
            if(teacherBaseInfos.size()>0){
                TeacherBaseInfoVo teacherBaseInfo = teacherBaseInfos.get(0);
                JSONObject scoreResult = assessmentService.getTeacherScoreAndRankedByUsernameAndDate(currentDate.getCurrentDate(),teacherBaseInfo.getTeachingOrgCode(),teacherId,projectId);

                Map<String,String> paramMap = new HashMap<>();
                paramMap.put("name",teacherBaseInfo.getRealname());

                paramMap.put("gender",dictTranslater.getDictValue(TeacherBaseInfoVo.class, "sex", String.valueOf(teacherBaseInfo.getSex())));
                Date birthDay = teacherBaseInfo.getBirthday();
                DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                String birthDayStr = dateFormat.format(birthDay);

                paramMap.put("currentDate", currentDate.getCurrentDate());
                paramMap.put("birthDay",birthDayStr.trim());

                paramMap.put("department",dictTranslater.getDictValue(TeacherBaseInfoVo.class, "teacherOrgCode", teacherBaseInfo.getTeachingOrgCode()));
                paramMap.put("position-type","专任教师");
                // ToDo 用户管理
//                paramMap.put("position-name",teacherBaseInfo.getPositionDescribe());
                paramMap.put("classtime-score",scoreResult.getJSONObject("classTime").getString("score"));
                paramMap.put("teaching-score",scoreResult.getJSONObject("teachingBuild").getString("score"));
                paramMap.put("honour-score",scoreResult.getJSONObject("honour").getString("score"));

                paramMap.put("research-score",scoreResult.getJSONObject("researchGL").getString("score"));
                paramMap.put("education-score",scoreResult.getJSONObject("educationAndService").getString("score"));
                paramMap.put("total-score",scoreResult.getString("totalScore"));
                paramMap.put("ranked-org",scoreResult.getString("totalOrgRanked"));
                TeacherIndexDocView teacherIndexDocView = SpringContextHolder.getBean(TeacherIndexDocView.class);
                ModelAndView mv = new ModelAndView(teacherIndexDocView);
                mv.addObject(TeacherIndexDocView.CONTENT, paramMap);
                return mv;
            } else {
                throw new RuntimeException("教师[" + teacherId + "]不存在");
            }

        } catch (NoSuchFieldException e){
            throw e;
        }
    }
}
